"""
<name>Points</name>
<icon></icon>
"""
from OWRpy import * 
import os.path, redREnviron
import redRi18n
_ = redRi18n.get_(package = 'compositions')

# signals
import libraries.base.signalClasses as signals
from libraries.base.signalClasses.RMatrix import RMatrix as redRRMatrix
from libraries.plotting.signalClasses.RPlotAttribute import RPlotAttribute as redRRPlotAttribute

# Qt widgets
from libraries.base.qtWidgets.comboBox import comboBox as redRcomboBox
from libraries.base.qtWidgets.spinBox import spinBox as redRSpinBox

# code
class RedRpoints(OWRpy): 
    globalSettingsList = ['commit']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        
        if not self.require_librarys(["graphics"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        
        # Python variables
        self.data = {}
        self.RFunctionParam_x = ''
        
        # Inputs
        self.inputs.addInput('id0', 'variables', redRRMatrix, self.processdata)#, multiple = True)
        
        # Outputs
        self.outputs.addOutput("points Output",_("points Output"), redRRPlotAttribute)
        
        
        self.RFunctionParam_pch = redRSpinBox(self.controlArea, label = "Point type:", min = 0, max=25, value = 1 )
        self.RFunctionParam_pcol = redRcomboBox(self.controlArea, label = 'Point color', displayLabel=True,
        items = [(1,'Black'), (2,'Red'), (3,'Green'), (4,'Blue'), (5,'Cyan'), (6,'Magenta'), (7,'Yellow'), (8,'Gray')])
        self.RFunctionParam_pcex = redRSpinBox(self.controlArea, label = "Point size:", min = 1, max=25, value = 1 )
        self.RFunctionParam_plwd = redRSpinBox(self.controlArea, label = "Point line width:", min = 1, max=25, value = 1 )
        
        self.commit = redRCommitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)

    def processdata(self, data):
        if data:
            self.RFunctionParam_x=data.getData()
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_x=''
        
    def commitFunction(self):
        if str(self.RFunctionParam_x) == '':
            return
        
        injection = []
        if unicode(self.RFunctionParam_pch.value()) != '':
            string = 'pch='+unicode(self.RFunctionParam_pch.value())+''
            injection.append(string)
        if unicode(self.RFunctionParam_pcol.currentId()) != '':
            string = 'col='+unicode(self.RFunctionParam_pcol.currentId())+''
            injection.append(string)
        if unicode(self.RFunctionParam_pcex.value()) != '':
            string = 'cex='+unicode(self.RFunctionParam_pcex.value())+''
            injection.append(string)
        if unicode(self.RFunctionParam_plwd.value()) != '':
            string = 'lwd='+unicode(self.RFunctionParam_plwd.value())+''
            injection.append(string)
            
            
        inj = ','.join(injection)
        
        newPlotAtt = redRRPlotAttribute(self, data = 'points(x=%s, %s)' % (str(self.RFunctionParam_x), inj))
        print '!Output', newPlotAtt
        self.rSend("points Output", newPlotAtt)